package org.u2ml.app.business.impl;

import java.util.List;

import javax.ws.rs.QueryParam;

import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import org.u2ml.app.business.ProjectDiagramService;
import org.u2ml.app.domain.ProjectDiagram;

@Component
@Transactional
public class ProjectDiagramServiceImpl implements ProjectDiagramService {

	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	public List<ProjectDiagram> get() {
		List<ProjectDiagram> list = sessionFactory.getCurrentSession()
    	.createQuery("from ProjectDiagram")
    	.list();
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public ProjectDiagram find(@QueryParam("oid") Long oid) {
		List<ProjectDiagram> list = sessionFactory.getCurrentSession()
        	.createQuery("from ProjectDiagram projectdiagram where projectdiagram.oid = :oid")
        	.setParameter("oid",oid)
        	.list();
		return (list.size()==0) ? null : list.get(0);
	}

	@SuppressWarnings("unchecked")
	public ProjectDiagram findByName(@QueryParam("name") String name) {
		List<ProjectDiagram> list = sessionFactory.getCurrentSession()
    	.createQuery("from ProjectDiagram projectdiagram where projectdiagram.name = :name")
    	.setParameter("name",name)
    	.list();
		return (list.size()==0) ? null : list.get(0);
	}
	
	@SuppressWarnings("unchecked")
	public List<ProjectDiagram> findByProject(Long projectOid) {
		List<ProjectDiagram> list = sessionFactory.getCurrentSession()
    	.createQuery("from ProjectDiagram projectdiagram where projectdiagram.projectId = :oid")
    	.setParameter("oid", projectOid)
    	.list();
		return list;
	}

	public ProjectDiagram save(ProjectDiagram c) {
		sessionFactory.getCurrentSession().save(c);
		return c;
	}

	public ProjectDiagram update(ProjectDiagram c) {
		sessionFactory.getCurrentSession().update(c);
		return c;
	}

	public void delete(ProjectDiagram c) {
		sessionFactory.getCurrentSession().delete(c);
	}	
}
